package UHFTest;

import UHF18.UHFReader;

import java.util.Timer;
import java.util.TimerTask;

public class UHFService {

    private UHFReader uhf = new UHFReader();
    String[] s = {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12"};
    String[] bauds = {"9600bps", "19200bps", "38400bps", "57600bps", "115200bps"};
    String[] powers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
            "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"};

    private int fCmdRet = 0x30;

    private boolean timeQyery = false;//定时器执行标志
    private boolean Qyeryflag = false;//单次询查结束标志

    /**
     * 遍历窗口，获取连接
     */
    public void openUHFReader() {
        for (int i = 0; i < s.length; i++) {
            int ComPort = i + 1;//端口号
            byte ComAddr = (byte) 255;
            //波特率为5--57600bps--出厂默认值
            byte baudRate = 5;
            fCmdRet = uhf.OpenByCom(ComPort, baudRate);
            if (fCmdRet == 0) {
                System.out.println("串口连接成功:" + s[i]);
                return;
            }
        }
    }

    /**
     * 关闭串口
     */
    public void closeUHFReader() {
        fCmdRet = uhf.CloseByCom();
        if (fCmdRet == 0) {
            System.out.println("关闭串口成功");
        }
    }

    /**
     * 开始
     */
    public void start() {
        timeQyery = true;
        timeVoid();
    }

    /**
     * 结束
     */
    public void end() {
        timeQyery = false;
    }

    public void timeVoid() {
        final Timer timer = new Timer();
        TimerTask tt = new TimerTask() {
            @Override
            public void run() {
                if (timeQyery) {
                    if (Qyeryflag) {
                        return;
                    }
                    Qyeryflag = true;
                    Inventory();
                    Qyeryflag = false;
                    timer.cancel();
                    timeVoid();
                }
            }
        };
        timer.schedule(tt, 50);
    }

    /**
     * 查询电子产品码清单
     */
    private void Inventory() {
        String[] EPC;
        EPC = uhf.Inventory();
        if (EPC != null) {
            for (int m = 0; m < EPC.length; m++) {
                String sepc = EPC[m];
                System.out.println("询查的标签:" + sepc);
            }
        }
    }


    /**
     * 设置功率：输出功率为0~26.
     */
    public void setPower(int power) {
        fCmdRet = uhf.SetPower(power);
        if (fCmdRet == 0) {
            System.out.println("设置功率成功:" + power);
        } else {
            System.out.println("设置功率失败");
        }
    }

    /**
     * 从EPC中读取数据
     *
     * @param EPC
     */
    public void readData(String EPC) {
        //起始地址
        byte WordPtr = (byte) (0xff & Integer.parseInt("00", 16));
        //字数
        byte Num = (byte) (0xff & Integer.parseInt("04", 16));
        //0-保留区，1-EPC区，2-TID区，3-用户区
        byte Mem = 3;
        //用户密码
        byte[] Psd = new byte[4];
        Psd = uhf.stringToByte("00000000");
        String result = uhf.ReadData(EPC, WordPtr, Num, Mem, Psd);
        System.out.println("数据:" + result);
    }


    /**
     * 填写数据
     */
    public void writeDate(String EPC) {
        byte WordPtr = (byte) (0xff & Integer.parseInt("00", 16));
        //0-保留区，1-EPC区，2-TID区，3-用户区
        byte Mem = 3;
        //用户密码
        byte[] Psd = new byte[4];
        Psd = uhf.stringToByte("00000000");
        String str_data = "";
        byte Num = (byte) (str_data.length() / 4);
        //字符串转为字节
        byte[] Data = uhf.stringToByte(str_data);
        fCmdRet = uhf.WriteData(EPC, WordPtr, Num, Data, Mem, Psd);
        if (fCmdRet == 0) {
            System.out.println("写数据成功");
        } else {
            System.out.println("写数据失败");
        }
    }

    /**
     * 专门写EPC数据
     *
     * @param epcString
     */
    public void writeEPC(String epcString) {
        String Psd = "00000000";
        fCmdRet = uhf.WriteEPC(epcString, Psd);
        if (fCmdRet == 0) {
            System.out.println("写EPC数据成功");
        } else {
            System.out.println("写EPC数据失败"+ Integer.toHexString(fCmdRet));
        }
    }

    public void openByTcp(String ip, Integer port) {
        fCmdRet = uhf.OpenByTcp(ip, port);
        if (fCmdRet == 0) {
            System.out.println("连接成功");
        }
    }

    public void closeByTcp() {
        fCmdRet = uhf.CloseByTcp();
        if (fCmdRet == 0) {
            timeQyery = false;
        }
    }

    public void setRegion() {
        //band的值1-Chiness Band2, 2-User Band, 3-Korean Band, 4-Euband
        int band = 1;
        fCmdRet = uhf.SetRegion(band);
        if (fCmdRet == 0) {
            System.out.println("设置频道成功");
        } else {
            System.out.println("设置频道失败");
        }
    }


}
